package com.demo.java.OD451_500.OD494;

import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【猜数字(B卷-100分)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146446628
 */
public class OdMain {
    // 获取当前猜测组合的猜测结果
    public static String singleCheck(String inputNum, String temp) {
        int countA = 0, countB = 0;
        int[] tempArr = new int[10];
        int[] inputArr = new int[10];

        for (int i = 0; i < inputNum.length(); i++) {
            if (inputNum.charAt(i) == temp.charAt(i)) {
                countA++;
            } else {
                tempArr[temp.charAt(i) - '0']++;
                inputArr[inputNum.charAt(i) - '0']++;
            }
        }

        for (int i = 0; i < 10; i++) {
            countB += Math.min(tempArr[i], inputArr[i]);
        }

        return countA + "A" + countB + "B";
    }

    // 判断当前组合是否满足所有的猜测
    public static boolean check(String temp, List<String[]> inputPairs) {
        for (String[] pair : inputPairs) {
            if (!pair[1].equals(singleCheck(pair[0], temp))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        List<String[]> inputPairs = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            inputPairs.add(scanner.nextLine().split(" "));
        }

        List<String> result = new ArrayList<>();
        String[] chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

        // 暴力遍历所有可能的四位数字组合
        for (String c1 : chars) {
            for (String c2 : chars) {
                for (String c3 : chars) {
                    for (String c4 : chars) {
                        String temp = c1 + c2 + c3 + c4;
                        if (check(temp, inputPairs)) {
                            result.add(temp);
                        }
                    }
                }
            }
        }

        // 多个可能的正确答案或者都不满足就是无效的
        System.out.println(result.size() != 1 ? "NA" : result.get(0));

        scanner.close();
    }
}